package com.chuantou.yuba.dao;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import com.chuantou.yuba.api.to.StartAdTO;
import com.chuantou.yuba.db.columns.CoverColumns;
import com.chuantou.yuba.db.util.CursorHelper;
import com.chuantou.yuba.db.util.SqliteWrapper;

public class CoverDao {

	/**
	 * 获取所有的广告封面信息(不包含图片流)
	 * 
	 * @param context
	 */
	public static final List<StartAdTO> getAllCover(Context context) {
		Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), CoverColumns.CONTENT_URI, CoverColumns.getSimpleProjection(), null, null,
				null);
		List<StartAdTO> covers = new ArrayList<StartAdTO>();
		try {
			if (cursor != null) {
				while (cursor.moveToNext()) {
					StartAdTO cover = readFromCursor(cursor);
					covers.add(cover);
				}
			}
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return covers;
	}

	public static final void insertCovers(Context context, List<StartAdTO> covers) {
		if (covers != null && !covers.isEmpty()) {
			for (StartAdTO cover : covers) {
				insertCover(context, cover);
			}
		}
	}

	public static final void deleteCover(Context context, long id) {
		SqliteWrapper.delete(context, context.getContentResolver(), CoverColumns.CONTENT_URI, CoverColumns._ID + "=?", new String[] { String.valueOf(id) });
	}

	public static final void insertCover(Context context, StartAdTO cover) {
		Cursor cursor = null;
		try {
			cursor = SqliteWrapper.query(context, context.getContentResolver(), CoverColumns.CONTENT_URI, new String[] { CoverColumns._ID }, CoverColumns._ID
					+ "=?", new String[] { String.valueOf(cover.getId()) }, null);
			if (cursor != null && cursor.getCount() <= 0) {
				ContentValues values = new ContentValues();
				values.put(CoverColumns._ID, cover.getId());
				values.put(CoverColumns.URL, cover.getImage());
				SqliteWrapper.insert(context, context.getContentResolver(), CoverColumns.CONTENT_URI, values);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
	}

	public static final int insertCoverBitmapByte(Context context, Bitmap bitmap, long id) {
		if (bitmap == null || bitmap.isRecycled() || id <= 0)
			return 0;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.JPEG, 100, out);
		byte[] data = out.toByteArray();
		ContentValues values = new ContentValues();
		values.put(CoverColumns.DATA, data);
		values.put(CoverColumns.DATA_COUNT, data.length);
		int count = SqliteWrapper.update(context, context.getContentResolver(), CoverColumns.CONTENT_URI, values, CoverColumns._ID + "=?",
				new String[] { String.valueOf(id) });
		return count;
	}

	public static final byte[] getCoverBitmap(Context context, long id) {
		Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), CoverColumns.CONTENT_URI, null, CoverColumns._ID + "=?",
				new String[] { String.valueOf(id) }, null);
		byte[] bytes = null;
		try {
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					bytes = CursorHelper.getBlob(cursor, CoverColumns.DATA);
				}
			}
		} finally {
			cursor.close();
		}
		return bytes;
	}

	private static StartAdTO readFromCursor(Cursor cursor) {
		StartAdTO cover = new StartAdTO();
		cover.setDataCount(CursorHelper.getLong(cursor, CoverColumns.DATA_COUNT, 0));
		cover.setImage(CursorHelper.getString(cursor, CoverColumns.URL, null));
		cover.setId(CursorHelper.getLong(cursor, CoverColumns._ID, 0));
		return cover;
	}
}
